﻿<?php

/* TravelTaste(C) 2009 - 2010
  *$ 20:35 2010-4-2
  * profile.php
 * Author: Yuhuang@ncic.ac.cn
*/


 
// 初始化页面
require_once "./init.php";
require_once "./sim.php";

$action = getgpc("action");
/* 强制让用户登录 */
if(1)
if (true != check_user_login("raywill", "111111"))
{
	die("wrong user/pass");
}

/* 登录用户 */

if (isset($_SESSION['login']) && $_SESSION['login']== true)
{
	/* 到达这一步的时候，可以确定系统已经取得了登录用户的基本信息*/
	$uid = $_SESSION['user']['uid'];
	//echo $_SESSION['user']['user'];
	$info = null;
	$state_list = null;
	$desc_list = null;
	$friend_list = null;
	switch($action)
	{
	default:
		/* fall through */
	case "basic":	/* 基本信息管理 */
		$state_list = $_SGLOBAL['db']->fetch_all("SELECT * FROM ".TT_DBTABLEPRE."state_list as t1 WHERE "
		."t1.uid=$uid ORDER BY t1.post_date LIMIT 100");
		
		$desc_list = $_SGLOBAL['db']->fetch_all("SELECT * FROM ".TT_DBTABLEPRE."friend_description as t1 WHERE "
		."t1.reply_to_uid=$uid ORDER BY t1.desc_time LIMIT 100");

		$info = array(
			"action" => "basic",
			//"action" => $action,
			"state_list" => $state_list,
			"desc_list" => $desc_list,
			"user_info" => $_SESSION['user'],
			"session" => $_SESSION
		);		
		break;
	case "history":

		
		$desc_list = $_SGLOBAL['db']->fetch_all("SELECT * FROM ".TT_DBTABLEPRE."friend_description as t1 WHERE "
		."t1.reply_to_uid=$uid ORDER BY t1.desc_time LIMIT 100");

		$info = array(
			"action" => $action,
			"state_list" => $state_list,
			"desc_list" => $desc_list,
			"user_info" => $_SESSION['user'],
			"session" => $_SESSION
		);		
		break;
	case "friend":	/*好友管理 */
		$friend_request = $_SGLOBAL['db']->fetch_all("SELECT * FROM ".TT_DBTABLEPRE."add_friend_request as t1, "
		.TT_DBTABLEPRE."user as t2 WHERE "
		."t1.host_id = $uid and t1.request_id = t2.uid ORDER BY t1.request_time LIMIT 100");

		/* 我的收藏 */
		$friend_list = $_SGLOBAL['db']->fetch_all("SELECT * FROM ".TT_DBTABLEPRE."friend_list as t1 , ".TT_DBTABLEPRE."user as t2  WHERE "
		."t1.host_id = $uid and t1.friend_id = t2.uid ORDER BY t1.become_friend_time LIMIT 100");
		/* 我的粉丝 */
		$fans_list = $_SGLOBAL['db']->fetch_all("SELECT * FROM ".TT_DBTABLEPRE."friend_list as t1 , ".TT_DBTABLEPRE."user as t2  WHERE "
		."t1.host_id = t2.uid and t1.friend_id = $uid ORDER BY t1.become_friend_time LIMIT 100");

		
		$info = array(
			"action" => $action,
			"fans_list" => $fans_list,
			"user_info" => $_SESSION['user'],
			"friend_list" => $friend_list,
			"session" => $_SESSION
		);		
		break;
	case "group": /* 群组管理 */
		$info = array(
			"action" => $action,
			"user_info" => $_SESSION['user'],			
			"session" => $_SESSION
		);	
		break;
	case "mail": /* 邮件管理 */
		$s = getgpc("sub");
		switch($s)
		{
			default:		
			case "in":
				/*
				 * t1: 信件内容
				 * t2: 发件人信息
				 */
				$box = $_SGLOBAL['db']->fetch_all("SELECT * FROM ".TT_DBTABLEPRE."inbox as t1, ".TT_DBTABLEPRE."user as t2 WHERE "
				."t1.receiver_id = $uid and t2.uid = t1.sender_id ORDER BY t1.msg_time LIMIT 100");
				$info = array(
					"action" => $action,
					"sub" => $s,
					"box" => $box,
					"session" => $_SESSION,
					"user_info" => $_SESSION['user']
				);		
				break;
			case "out":
				/*
				 * t1: 信件内容
				 * t2: 收件人信息
				 */		
				$box = $_SGLOBAL['db']->fetch_all("SELECT * FROM ".TT_DBTABLEPRE."outbox as t1, ".TT_DBTABLEPRE."user as t2 WHERE "
				."t1.sender_id = $uid and t2.uid = t1.receiver_id ORDER BY t1.msg_time LIMIT 100");	
				$info = array(
					"action" => $action,
					"sub" => $s,
					"box" => $box,
					"user_info" => $_SESSION['user'],
					"session" => $_SESSION
				);			
				break;
			case "new":
				$toid = getgpc("toid");
				$type = "undefined";				
				if($toid != null)
				{
					$type = "defined";
					$receiver_info = $_SGLOBAL['db']->fetch_all("SELECT * FROM ".TT_DBTABLEPRE."user as t1 WHERE "
					."t1.uid = $toid LIMIT 1");	
				}
				else
				{
					$receiver_info = $_SGLOBAL['db']->fetch_all("SELECT * FROM ".TT_DBTABLEPRE."friend_list as t1 , ".TT_DBTABLEPRE."user as t2  WHERE "
					."t1.host_id = $uid and t1.friend_id = t2.uid ORDER BY t1.become_friend_time LIMIT 100");

				}
				$info = array(
					"action" => $action,
					"sub" => $s,
					"type" => $type,
					"receiver_info" => $receiver_info,
					"user_info" => $_SESSION['user'],
					"session" => $_SESSION
				);			
				break;
			case "sent":
				/* Deliver Message to Receiver's Mail Box */
				
				$info = array(
					"action" => $action,
					"sub" => $s,
					"user_info" => $_SESSION['user'],
					"session" => $_SESSION
				);			
		} /* end switch of sub */
		break;

	}
	
	//var_dump($info);
	
	// 最近访客
	$recent_visitor = $_SGLOBAL['db']->fetch_all("SELECT * FROM ".TT_DBTABLEPRE."recent_visitor WHERE host_id = $uid ORDER BY visit_time  DESC LIMIT 9");
	
	// 好友列表
	$friend_list =  $_SGLOBAL['db']->fetch_all("SELECT * FROM ".TT_DBTABLEPRE."friend_list WHERE host_id = $uid ORDER BY become_friend_time LIMIT 9");

	$info += array("recent_visitor"=>$recent_visitor, "friend_list" => $friend_list);
	
	$_SGLOBAL['view']->assign('image_root', TT_ROOT.'./images/icon');
	$_SGLOBAL['view']->assign("info",$info);
	$_SGLOBAL['view']->display('profile_std.tpl');

}
/* 未登录用户 */
else
{

	die("Please login to view profile.php page ");

}



function display_login_page($get)
{
	global $_SGLOBAL;
	// 显示页面

}

/*
 * 获得guest用户的目标旅行位置
 */
function determin_guest_distination()
{
	

	/* 先尝试从SESSION中获取用户选择的位置 
	 * 获取失败，则设置为空 
	 */
	if (isset($_SESSION['distination_setting']))
	{
		$d = getgpc("distination");
		if (null != $d)	/*设置了新的地址, 则更新session */
		{
			$_SESSION['distination_setting'] = $d;
		}
		return $_SESSION['distination_setting'];
	}
	else
	{
		/* 尝试读取用户输入，看是否在本次设置位置 */
		$d = getgpc("distination"); /*设置了地址, 保存进session */
		if (null != $d)
		{
			$_SESSION['distination_setting'] = $d;
			return $_SESSION['distination_setting'];
		}
		return null;
	}
}

function determin_guest_location()
{
	if (isset($_SESSION['location_setting']))
	{
		$d = getgpc("location");
		if (null != $d)
		{
			$_SESSION['location_setting'] = $d;
		}
		return $_SESSION['location_setting'];
	}
	else
	{
		/* 尝试读取用户输入，看是否在本次设置位置 */
		$d = getgpc("location");
		if (null != $d)
		{
			$_SESSION['location_setting'] = $d;
			return $_SESSION['location_setting'];
		}
	}
	return null;
}


?>